<template>
  <div>
    <div>题目</div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      aaa: [
        {
          option: null,
          id: 243,
          question: 'wwewq2',
          subject_id: 85,
          question_type: 2,
          optionA: '2',
          optionB: '2',
          optionC: '',
          optionD: '',
          optionE: '',
          answer: ['A'],
          explain: '的',
          score: 5,
          status: 'error',
          yetChoose: 'A',
          isCanClick: false,
          isShowParse: false,
          showSubmitBtn: false
        }
      ]
    }
  },
  created() {
    // this.init()
    // this.transfrom()
    this.getlist()
  },
  methods: {
    init() {
      var self = this
      let ww = {
        status: 'success',
        code: 200,
        data: {
          test: [
            {
              menu: {
                A: {
                  label: '1',
                  className: 'option-error'
                },
                B: {
                  label: '2',
                  className: ''
                },
                C: {
                  label: '2',
                  className: ''
                },
                D: {
                  label: '0',
                  className: ''
                }
              }
            }
          ]
        }
      }
      let ccc = ['A', 'B', 'C', 'D', 'E']
      ww.data.test.forEach((item, index) => {
        self.$set(self.aaa, ccc[index], item)
      })
      ww.data.test.push(self.aaa)
      console.log(ww)
    },
    transfrom() {
      let originData = {
        status: 'success',
        code: 200,
        data: {
          test: [
            {
              option: null,
              id: 243,
              question: 'wwewq2',
              subject_id: 85,
              question_type: 2,
              optionA: '2',
              optionB: '2',
              optionC: '',
              optionD: '',
              optionE: '',
              answer: ['A'],
              explain: '的',
              score: 5
            },
            {
              option: null,
              id: 245,
              question: 'wwewq3',
              subject_id: 85,
              question_type: 3,
              optionA: '1',
              optionB: '2',
              optionC: '2',
              optionD: '0',
              optionE: '',
              answer: ['A', 'B'],
              explain: '的',
              score: 5
            },
            {
              option: ['A'],
              id: 244,
              question: 'wwewq1',
              subject_id: 85,
              question_type: 1,
              optionA: '1',
              optionB: '2',
              optionC: '2',
              optionD: '0',
              optionE: '',
              answer: ['B'],
              explain: '的',
              score: 5
            }
          ],
          not_done: [243, 245],
          test_id: 436,
          test_paper_info: {
            id: 85,
            title: '12333',
            time: 1559,
            standard_answer_time: 60,
            type: 1,
            model: 2,
            begin_time: '2021-01-26 14:14:37',
            end_time: '2021-02-28 14:14:37',
            pause: 1611654013
          }
        }
      }
      let not_list = []
      let yet_list = []
      originData.data.test.forEach((el, e) => {
        if (!el.option) {
          not_list.push(e)
        } else {
          yet_list.push(e)
        }
      })
      console.log('not_list', not_list)
      console.log('yet_list', yet_list)
    },
    getlist() {
      let three = {
        status: 'success',
        code: 200,
        data: {
          test: [
            {
              option: ['A'],
              id: 243,
              question: 'wwewq2',
              subject_id: 85,
              question_type: 2,
              optionA: '2',
              optionB: '2',
              optionC: '',
              optionD: '',
              optionE: '',
              answer: ['A'],
              explain: '的',
              score: 5
            },
            {
              option: ['A', 'B'],
              id: 245,
              question: 'wwewq3',
              subject_id: 85,
              question_type: 3,
              optionA: '1',
              optionB: '2',
              optionC: '2',
              optionD: '0',
              optionE: '',
              answer: ['A', 'B'],
              explain: '的',
              score: 5
            },
            {
              option: ['A'],
              id: 244,
              question: 'wwewq1',
              subject_id: 85,
              question_type: 1,
              optionA: '1',
              optionB: '2',
              optionC: '2',
              optionD: '0',
              optionE: '',
              answer: ['B'],
              explain: '的',
              score: 5
            }
          ],
          not_done: [243, 245],
          test_id: 436,
          test_paper_info: {
            id: 85,
            title: '12333',
            time: 1559,
            standard_answer_time: 60,
            type: 1,
            model: 2,
            begin_time: '2021-01-26 14:14:37',
            end_time: '2021-02-28 14:14:37',
            pause: 1611654013
          }
        }
      }

      let yetChoose = []
      // let status = []
      three.data.test.forEach((item, index) => {
        // 将option赋值给yetChoose
        if (item.question_type == 1 || item.question_type == 2) {
          if (Array.isArray(item.option)) {
            item.yetChoose = item.option[0]
          } else {
            if (!item.option) {
              item.yetChoose = ''
            }
          }
        }
        if (item.question_type === 3) {
          if (Array.isArray(item.option)) {
            item.yetChoose = item.option
          } else {
            item.yetChoose = []
          }
        }
        // console.log(item)
        // 选项变形
        item.menu = {}
        for (let k in item) {
          // 找到option开头的字段
          if (
            item.hasOwnProperty(k) &&
            k !== 'option' &&
            k.indexOf('option') > -1
          ) {
            if (item[k]) {
              item.menu[k.replace('option', '')] = {}
              item.menu[k.replace('option', '')]['label'] = item[k]
              item.menu[k.replace('option', '')]['className'] = ''
            }
          }
        }
          // console.log('k', k)
          // console.log('k', item[k])

          // 判断option和answer是否相等
          // 1.2 是判断字符串
          // 3 是判断数组
          if ([1, 2].includes(item.question_type)) {
            // 字符串判断
            if (item.yetChoose) {
              if (item.yetChoose === item.answer[0]) {
                item.menu[item.yetChoose].className = 'option-success'
              } else {
                item.menu[item.yetChoose].className = 'option-error'
              }
            }
          }
          if ([3].includes(item.question_type)) {
            // 数组判断
            if (item.yetChoose && item.yetChoose.length > 0) {
              // Object.prototype.toString.call(ar1) === '[object Array]'
              if (
                JSON.stringify(item.yetChoose) == JSON.stringify(item.answer)
              ) {
                item.yetChoose.forEach(el => {
                  item.menu[el].className = 'option-success'
                })
              } else {
                item.yetChoose.forEach(el => {
                  item.menu[el].className = 'option-error'
                })
              }
            }
          }
      })
      console.log('test', three.data.test)
    }
  }
}
</script>
<style lang="less" scoped>
</style>
